package com.atguigu.spzx.config;


import com.atguigu.spzx.interceptor.LoginAuthInterceptor;
import com.atguigu.spzx.properties.UserAuthProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;


/**
 * 跨域
 * 注册拦截器
 */


@Configuration
public class WebMvcConfiguration implements WebMvcConfigurer {


    @Autowired
    private LoginAuthInterceptor loginAuthInterceptor;

    @Autowired
    private UserAuthProperties authProperties;


    //跨域问题
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")  //表示允许哪些@RequestMapping路径跨域
                .allowedHeaders("*")
                .allowCredentials(true) //是否可以携带cookie
                .allowedOriginPatterns("*")
                .allowedMethods("*");
    }
//拦截器
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(loginAuthInterceptor)
                .excludePathPatterns(authProperties.noAuthUrls) //支持List<>
                .addPathPatterns("/**");
    }
}
